Vue.component('xcform_control_objselect', {
    template: '<el-form-item\
                  :class="value.fieldjsonObj.xcform_prop_required.value?\'required\':\'\'"\
                  :prop="value.fieldcode" \
                  :rules="control.rules" \
                  :label="value.fieldtitle"> \
                  <el-select  :key="value.fieldcode" clearable filterable remote :remote-method="control_clickFilter" @change="control_change"\
                    v-model="value.value" :placeholder="value.fieldjsonObj.xcform_base_placeholder.value" \
                    :popper-append-to-body="false" :disabled="control.disabled" > \
                    <el-option v-for="option in control.options" :key="option.key" :label="option.label" :value="option.key"></el-option> \
                    <div> \
                      <el-pagination layout="prev, pager, next" background \
                        :hide-on-single-page="true" :page-count="control.totalpage"  @current-change="change_page"> \
                      </el-pagination> \
                    </div> \
                  </el-select> \
                </el-form-item>', //:pager-count="control.pagerCount"
    props: {
        value: {
            type: Object,
        },
        disabled: {
            type: Boolean,
        },
        fieldfulljson: {
            type: Array,
        },
        xcform_id: {
            type: Number,
        }
    },
    data() {
        var validateFunction = (rule, value, callback) => {
            var that = this;
            that.value.value = (that.value.value) ? that.value.value : "";

            if (that.value.fieldjsonObj.xcform_prop_required.value) {
                if (that.value.value.length == 0) {
                    return callback(new Error('不能为空'));
                }
            }
            return callback();
        };
        return {
            control: {
                disabled: false,
                rules: [{
                    validator: validateFunction,
                    trigger: 'blur'
                }],
                pageNumber: 0,
                totalpage: 1,
                filterString: "",
                options: [],
            }
        };
    },
    created() {
        var that = this;
        that.control.disabled = that.disabled;
        if (that.value.fieldjsonObj.xcform_prop_readonly.value) {
            that.control.disabled = true;
        }



    },
    mounted() {
        var that = this;
        that.control_clickFilter(that.control.filterString);
    },
    methods: {
        change_page(pageNumber) {
            var that = this;
            that.control.pageNumber = pageNumber;
            that.control_clickFilter(that.control.filterString);
        },
        control_clickFilter(filterString) {
            var that = this;

            that.control.filterString = filterString;
            if (true) {
                Fast.api.ajax({
                    url: Fast.api.fixurl(Config.controlPath + "queryTabledataListByFieldcode"),
                    method: 'get',
                    data: {
                        xcform_id: that.xcform_id,
                        fieldcode: that.value.fieldcode,
                        pageNumber: that.control.pageNumber,
                        filtervalue: ""
                    }
                }, function (data, ret) {
                    that.control.totalpage = data.totalpage;
                    that.control.options = data.list;

                    var options = [];
                    for (var index in data.list) {
                        var item = data.list[index];
                        var row = [];
                        for (var index2 in that.value.fieldjsonObj.xcform_base_selecttablefield.showfields) {
                            var field = that.value.fieldjsonObj.xcform_base_selecttablefield.showfields[index2];
                            row.push(item[field]);
                        }
                        options.push({
                            key: item.id,
                            label: row.join(' ')
                        });
                    }
                    that.control.options = options;
                    return false;
                });
            }
        },
        control_change(){
            var that = this;
            if (that.value.value != null && that.value.value != "") {
                Fast.api.ajax({
                    url: Fast.api.fixurl(Config.controlPath + "getTabledataRowByFieldcodeAndDataid"),
                    method: 'get',
                    data: {
                        xcform_id: that.xcform_id,
                        fieldcode: that.value.fieldcode,
                        data_id: that.value.value
                    }
                }, function (data, ret) {
                    let datarow = data.row;
                    for (var index in that.fieldfulljson) {
                        var field = that.fieldfulljson[index];
                        if(field['fieldjsonObj']['xcform_base_defaultvalue']!=null){
                            var defaultvalue = field['fieldjsonObj']['xcform_base_defaultvalue'];
                            if (defaultvalue['type'] == "objselect_field") {
                                if (defaultvalue['objselect_field']['fieldcode'] == that.value.fieldcode) {
                                    that.fieldfulljson[index]["value"] = datarow[defaultvalue['objselect_field']['targetObject_fieldcode']];
                                }
                            }
                        }
                    }
                    return false;
                });
            }
        },
    }
});